package com.lancoo.znbkxx.teacher.diagnosis

import android.graphics.Canvas
import android.graphics.Color
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.renderer.YAxisRenderer
import com.github.mikephil.charting.utils.Transformer
import com.github.mikephil.charting.utils.ViewPortHandler

/**
 * @author : Admin
 * @date   : 2020/12/18
 * @desc   :
 */
class MyLineChartRender(viewPortHandler: ViewPortHandler?, yAxis: YAxis?, trans: Transformer?) :
        YAxisRenderer(viewPortHandler, yAxis, trans) {
    override fun drawYLabels(c: Canvas?, fixedPosition: Float, positions: FloatArray?,
        offset: Float) {
        val from = if (mYAxis.isDrawBottomYLabelEntryEnabled) 0 else 1
        val to = if (mYAxis.isDrawTopYLabelEntryEnabled) mYAxis.mEntryCount else mYAxis.mEntryCount - 1
        for (i in from..to) {
            val text = mYAxis.getFormattedLabel(i)
            if (text.isNotEmpty()) {
                if (text == "0" || text == "10") {
                    mAxisLabelPaint.color = Color.parseColor("#788aa4")
                    c?.drawText(text, fixedPosition, positions!![i * 2 + 1] + offset,
                        mAxisLabelPaint)
                } else {
                    mAxisLabelPaint.color = Color.parseColor("#788aa4")
                    c?.drawText(text, text.length - 1, text.length, fixedPosition,
                        positions!![i * 2 + 1] + offset, mAxisLabelPaint)
                    mAxisLabelPaint.color = Color.parseColor("#ff9900")
                    c?.drawText(text, if (text.contains(".")) text.length - 4 else text.length - 2,
                        text.length - 1,
                        fixedPosition - mAxisLabelPaint.measureText(text, text.length - 1,
                            text.length), positions!![i * 2 + 1] + offset, mAxisLabelPaint)
                    mAxisLabelPaint.color = Color.parseColor("#788aa4")
                    c?.drawText(text, 0,
                        if (text.contains(".")) text.length - 4 else text.length - 2,
                        fixedPosition - mAxisLabelPaint.measureText(text,
                            if (text.contains(".")) text.length - 4 else text.length - 2,
                            text.length), positions!![i * 2 + 1] + offset, mAxisLabelPaint)
                }
            }
        }
    }
}